<template>
  <div class="app">
    <div class="search-bar">
      <el-form :model="searchData" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="公司名称" style="margin-bottom: 5px;">
              <el-input v-model="searchData.companyName" size="medium" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="公司代码" style="margin-bottom: 5px;">
              <el-input v-model="searchData.cmhkId" size="medium" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <div style="margin-top: 3px;">
              <el-button type="primary" size="small" @click="handleSearch">查询</el-button>
              <el-button size="small" @click="handleClear">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="add-form">
      <el-row :gutter="10">
        <el-form :model="addForm" :rules="rules" ref="addForm" label-width="auto">
        <el-col :span="3">
          <el-form-item label=" " prop="companyName" style="margin-bottom: 0;">
            <el-input v-model="addForm.companyName" placeholder="公司名称" size="small"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item label=" " prop="cmhkId" style="margin-bottom: 0;">
            <el-input v-model="addForm.cmhkId" placeholder="公司代码" size="small"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label=" " prop="easName" style="margin-bottom: 0;">
           <el-input v-model="addForm.easName" placeholder="EAS机构名称" size="small"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label=" " prop="easCode" style="margin-bottom: 0;">
           <el-input v-model="addForm.easCode" placeholder="EAS机构代码" size="small"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label=" " prop="oaName" style="margin-bottom: 0;">
           <el-input v-model="addForm.oaName" placeholder="OA组织机构名称" size="small"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label=" " prop="oaCode" style="margin-bottom: 0;">
           <el-input v-model="addForm.oaCode" placeholder="OA组织机构代码" size="small"></el-input>
          </el-form-item>
        </el-col>
         <el-col :span="2">
          <el-button type="primary" size="small" style="margin-top: 5px;" @click="handleAdd('addForm')">新增</el-button>
        </el-col>
        </el-form>
      </el-row>
      
    </div>
    <div ref="ref2" class="table-box">
      <tableComponent
        class="table"
        :height="height"
        :data="tableData || []"
        :columns="tableColumns"
        :ifNoEditColumns="ifNoEditColumns"
        @change="tablePage"
        :table-option="tableOption"
        :page-option="pageOption"
        :total="total"
      >
        <template scope="scope">
          <el-button v-if="scope.scope.row.isEditing" @click="saveRow(scope.scope.row)" type="text" size="mini">保存</el-button>
          <template v-if="!scope.scope.row.isEditing">
            <el-button @click="editRow(scope.scope.row)" type="text" size="mini">编辑</el-button>
            <el-button @click="deleteRow(scope.scope.row)" type="text" size="mini">删除</el-button>
          </template>
        </template>
      </tableComponent>
    </div>
  </div>
</template>

<script>
import tableComponent from '@/components/Table/table'
import { tableColumns, ifNoEditColumns } from './index.js'
import { getDataMap, addDataMap, editDataMap, delDataMap } from '@/api/modules/systemManage.js'

export default {
  components: {
    tableComponent
  },
  data() {
    return {
      searchData: {
        companyName: '',
        cmhkId: '',
        page: {
          pageNum: 1,
          pageSize: 10
        }
      },
      addForm: {
        companyName: '',
        cmhkId: '',
        easName: '',
        easCode: '',
        oaName: '',
        oaCode: ''
      },
      rules: {
        companyName: [
          { required: true, message: '请输入公司名称', trigger: 'change' },
        ],
        cmhkId: [
          { required: true, message: '请输入公司代码', trigger: 'change' }
        ],
        easName: [
          { required: true, message: '请输入EAS机构名称', trigger: 'change' }
        ],
        easCode: [
          { required: true, message: '请输入EAS机构代码', trigger: 'change' }
        ],
        oaName: [
          { required: true, message: '请输入OA组织机构名称', trigger: 'change' }
        ],
        oaCode: [
          { required: true, message: '请输入OA组织机构代码', trigger: 'change' }
        ]
      },
      height: null,
      tableColumns,
      ifNoEditColumns,
      tableData: [],
      tableOption: {
        isShowOperate: true,
        NoInlineEdit: true,
        operateWidth: '100px'
      },
      pageOption: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.height = this.$refs.ref2.offsetHeight
    })
    window.onresize = () => {
      this.height = this.$refs.ref2.offsetHeight
    }
    this.initData(this.searchData)
  },
  destroyed(){
    window.onresize = null
  },
  methods: {
    initData(params) {
      getDataMap(params).then(res => {
        if (res.data.code === '200') {
          let list =  res.data.data.list || []
          this.tableData = list.map(value => {
            return Object.assign({}, value, { isEditing: false })
          })
          this.total = res.data.data.total
        }
      })
    },
    // 新增
    handleAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addDataMap(this.addForm).then(res => {
            if (res.data.code === '200') {
              this.$message.success('新增成功')
              this.initData(this.searchData)
              this.resetForm('addForm')
            } else if (res.data.code === '500') {
              this.$message.error(res.data.message)
            }
          })
        } else {
          return false
        }
      })
    },
    // 查询
    handleSearch() {
      this.initData(this.searchData)
    },
    // 分页
    tablePage(val) {
      this.searchData.page = val
      this.initData(this.searchData)
    },
    // 重置
    handleClear() {
      this.searchData.companyName = ''
      this.searchData.cmhkId = ''
    },
    saveRow(row) {
      console.log(row, 'save')
      if (row.easName === '') {
        this.$message.error('eas机构名称不能为空')
      } else if (row.easCode === '') {
        this.$message.error('eas机构代码不能为空')
      } else if (row.oaName === '') {
        this.$message.error('oa机构名称不能为空')
      } else if (row.oaCode === '') {
        this.$message.error('oa机构代码不能为空')
      } else {
        editDataMap(row).then(res => {
          if (res.data.code === '200') {
            this.$message.success('修改成功')
            row.isEditing = false
            this.initData(this.searchData)
          }
        })
      }
    },
    editRow(row) {
      row.isEditing = true
    },
    deleteRow(row) {
      console.log(row, 'del')
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const data = {
          cmhkId: row.cmhkId
        }
        delDataMap(data).then(res => {
          if (res.data.code === '200') {
            this.$message.success('删除成功')
            this.initData(this.searchData)
          } else {
            this.$message.error(res.data.message)
          }
        })
      }).catch(() => {
        return false
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
  .app {
    height: 100%;
    .search-bar {
      width: calc(100% - 60px);
      background-color: #ffffff;
      margin-bottom: 10px;
      padding: 10px 30px;
    }
    .table-box {
      width: 100%;
      height: calc(100vh - 280px);
      .table {
        height: 100%;
      }
    }
    .add-form {
      width: calc(100% - 40px);
      padding: 10px 20px;
      background-color: #ffffff;
      margin-bottom: 10px;
    }
  }
</style>